﻿using System.Net.Sockets;
using System.Net;
using Google.Protobuf;

namespace NetClient
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Client Start!");

            string host = "127.0.0.1";
            int port = 32510;
            IPAddress address = IPAddress.Parse(host);
            IPEndPoint ipEndPoint = new IPEndPoint(address, port);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(ipEndPoint);

            Console.WriteLine("Client Connect!");


            Package package = new Package();
            package.Request = new Request();
            package.Request.UserLoginRequest = new UserLoginRequest();
            package.Request.UserLoginRequest.Username = "xingge";
            package.Request.UserLoginRequest.Password = "123";


            MemoryStream rawOutPut = new MemoryStream();
            CodedOutputStream output = new CodedOutputStream(rawOutPut);

            package.WriteTo(output);
            output.Flush();
            SendMessage(socket, rawOutPut.ToArray());


        }

        static void SendMessage(Socket socket, byte[] body)
        {
            byte[] length = BitConverter.GetBytes(body.Length);
            socket.Send(length);
            socket.Send(body);
        }
    }
}